package com.example.fszq.volleydemo1.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;

import com.example.fszq.volleydemo1.R;
import com.example.fszq.volleydemo1.layout.MyGridView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fszq on 2015/12/27.
 */
public class ImageSelectedAdapter extends ArrayAdapter<String> {
    private int resourceId;
    //ImageLoader imageLoader = ImageLoader.getInstance();

    String imgSrc = "";
    int objCount = 0;
    Context context;
    //List<String> objects = new ArrayList<>();

    /**
     * 文件夹路径
     */
    private String mDirPath;

    public  ImageSelectedAdapter(Context context, int ViewResourceId, List<String> objects) {
        super(context, ViewResourceId, objects);
        objCount = objects.size();
        //this.objects = objects;
        resourceId = ViewResourceId;
        this.context = context;
    }

    //@Override
    //public int getCount() {
    //    objCount = objects.size();
        //objCount ++;
    //    return objCount;
    //}

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        //Log.d("getCount:", getCount() + "" );
        imgSrc = getItem(position);
        ViewHolder viewHolder;

            Log.d("selectimagegetkey:", position + "," + imgSrc);

            if (convertView == null) {
                view = LayoutInflater.from(getContext()).inflate(resourceId, null);
                viewHolder = new ViewHolder();

                viewHolder.sImage = (ImageView) view.findViewById(R.id.id_selected_image);
                viewHolder.sImage.setTag(position);
                view.setTag(viewHolder); // 将ViewHolder存储在View中

            } else {
                view = convertView;
                viewHolder = (ViewHolder) view.getTag(); // 重新获取ViewHolder
            }

            //if( position + 1 < objCount ) {
//            if (imgSrc.equals("default") == false) {
//                Log.d("viewHolder.sImage:", viewHolder.sImage + "," + viewHolder.sImage.getTag().toString());
//                imageLoader.displayImage("file://" + imgSrc, viewHolder.sImage);
//            } else {
//                Log.d("objcount", objCount + "");
//                Log.d("viewHolder.default:", viewHolder.sImage + "," + viewHolder.sImage.getTag().toString());
//                viewHolder.sImage.setImageResource(R.mipmap.icon_addpic_unfocused);
//            }

            if(imgSrc.equals("default")){
                viewHolder.sImage.setImageResource(R.mipmap.icon_addpic_unfocused);
            }else{
//                viewHolder.sImage.setImageResource(R.mipmap.ic_launcher);
//                imageLoader.displayImage("file://" + imgSrc, viewHolder.sImage);
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 2;
                Bitmap bm = BitmapFactory.decodeFile(imgSrc,options);
                viewHolder.sImage.setImageBitmap(bm);
            }

            return view;

    }

    class ViewHolder {
        ImageView sImage;

    }
}
